
from datetime import datetime

from SimTrader.constants import Exchange


def make_symbol(instrumnet_id, exchange):
    """
    make symbol based on instrument and exchange
    :param instrumnet_id: str
    :param exchange: str
    :return: symbol
    """
    return "".join((str(exchange).strip(), str(instrumnet_id).strip()))


def make_symbol_by_ins(instrument_id):
    """
    make symbol based on instrument
    :param instrument_id: str
    :return: symbol or None if illegal
    """
    instrument_id = str(instrument_id).strip()
    if len(instrument_id) != 6:
        return
    if instrument_id.startswith("60"):
        return "".join((Exchange.SH, instrument_id))
    elif instrument_id.startswith("00") or instrument_id.startswith("30"):
        return "".join((Exchange.SZ, instrument_id))
    else:
        return instrument_id


def extract_exchange_by_ins(instrument_id):
    """
    extract exchange based instrument
    :param instrument_id: str
    :return: Exhange or None if illegal
    """
    if len(instrument_id) != 6:
        return
    if instrument_id.startswith("60"):
        return Exchange.SH
    elif instrument_id.startswith("00") or instrument_id.startswith("30"):
        return Exchange.SZ
    else:
        return


def debug_print(content='', dt_format="%Y-%m-%d %H:%M:%S"):
    """
    print info for debug
    :param content: info <str ir object>
    :param dt_format: datetime_format
    :return: None
    """
    print("[%s] %s" % (datetime.now().strftime(dt_format), content))